package com.syedu.cels.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import com.alibaba.fastjson.JSON;
import com.syedu.cels.dao.BaseDao;
import com.syedu.cels.entity.*;
import com.syedu.cels.exception.VocabularyFileNotFoundException;
import com.syedu.cels.exception.WordFileNotFoundException;
import com.syedu.cels.util.BeanFactory;

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.*;

/**
 * description：业务类
 * @author Administrator
 * @date 2022/3/21 16:13
 */
public class BaseService {
    /**创建Dao的实现类对象,此实现类可以进行不同版本的切换*/
    BaseDao dao = (BaseDao) BeanFactory.getBean("baseDao");

    /**
     * 分页显示指定首字母的单词列表,每页显示10条记录
     */
    public List<Word> findWordsByPage(String firstLetter, int pageNow, int pageSize) {
        Map<Character, List<Word>> wordsMap = getWordsMap();
        List<Word> words = wordsMap.get(firstLetter.charAt(0));
        return CollUtil.page(pageNow - 1, pageSize, words);
    }

    /**
     * 读取所有的词汇
     */
    public List<Vocabulary> findVocabulariesByPage(int pageNow, int pageSize) {
        List<Vocabulary> list = null;
        try {
            list = CollUtil.page(pageNow, pageSize, dao.selectAllVocabularies());
        } catch (VocabularyFileNotFoundException e) {
            e.printStackTrace();
        }
        return list;
    }

    /**
     * 把List集合转换成Map集合
     */
    public Map<Character, List<Word>> getWordsMap() {
        List<Word> words = null;
        Map<Character, List<Word>> map = null;
        try {
            words = dao.selectAllWords();
            if (words != null && words.size() > 0) {
                map = new HashMap<>();
                for (Word word : words) {
                    if (map.containsKey(word.getFirstLetter())) {
                        map.get(word.getFirstLetter()).add(word);
                    } else {
                        List<Word> subList = new ArrayList<>();
                        subList.add(word);
                        map.put(word.getFirstLetter(), subList);
                    }
                }
            }
        } catch (WordFileNotFoundException e) {
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 获取词汇总数量
     */
    public int vocabularyCount() {
        int count = 0;
        try {
            count = dao.selectAllVocabularies().size();
        } catch (VocabularyFileNotFoundException e) {
            e.printStackTrace();
        }
        return count;
    }

    /**
     * 根据参数来创建试卷
     * num: 题目数量
     */
    public List<Base> createTest(int num) {
        //创建题目的总容器
        List<Base> total = new ArrayList<>();
        List<Base> result = new ArrayList<>();
        try {
            //所有单词
            List<Word> words=dao.selectAllWords();
            //所有词汇
            List<Vocabulary> vocabularies = dao.selectAllVocabularies();
            //分别遍历单词和词汇集合,把所有数据添加到total中
            for (Word w : words) {
               total.add(w);
            }
            for (Vocabulary v : vocabularies) {
                total.add(v);
            }
            //打乱集合数据
            Collections.shuffle(total);
            //出题
            for (int i = 1; i <= num; ) {
                int idx = (int) (Math.random() * total.size());
                Base base = total.get(idx);
                if (base == null) {
                    continue;
                }
                result.add(base);
                total.set(idx, null);
                i++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 把测试结果写入到记录文件中
     */
    public void logTest(TestResult testResult) {
        String jsonString = JSON.toJSONString(testResult);
        try (PrintWriter printWriter = new PrintWriter(new FileWriter("data/r.dic", true), true)) {
            printWriter.println(jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 展示测试记录
     */
    public void showTestResult() {
        List<String> lines = FileUtil.readLines(new File("data\\r.dic"), "UTF-8");
        //FileUtils.readLines(new File("data/r.dic"), Charset.defaultCharset());
        //List<String> lines = Files.readAllLines(Path.of(""), Charset.defaultCharset());
        for (int i = lines.size() - 1; i >= 0; i--) {
            //把读取到的JSON字符串转换成Java对象
            TestResult testResult = JSON.parseObject(lines.get(i), TestResult.class);
            System.out.println(testResult);
            System.out.println("-------------------------------------");
        }
    }
    public void shen(String shen){
        String jsonString=JSON.toJSONString(shen);
        try (PrintWriter printWriter = new PrintWriter(new FileWriter("data/s.dic", true), true)) {
            printWriter.println(jsonString);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void showshen(){
        List<String> lines = FileUtil.readLines(new File("data\\s.dic"), "UTF-8");
        System.out.println(lines);
            System.out.println("-------------------------------------");

    }
    public List<Base> sccs(int num) {
           List<String> total = new ArrayList<>();
           List<Base> result = new ArrayList<>();
        try {
            Map<en,cns> lines = FileUtil.readLines(new File("data\\s.dic"), "UTF-8");
            for (Base s:lines){
                total.add(s);
            }

            Collections.shuffle(lines);
            for (int i = 1; i <= num; ) {
                int idx = (int) (Math.random() * total.size());
               Base base = total.get(idx);
                if (base == null) {
                    continue;
                }
                result.add(base);
                total.set(idx, null);
                i++;
            }
        }catch (Exception e) {
            e.printStackTrace();
        }


    }


}



